﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using System.Data.SqlClient;
using TierApplication;


namespace OrderMgr.OrderInfo
{
    public partial class frmSaleInvoiceDetailInfo : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _InvoiceID = "";
        public string _OrderID = "";
        public Utility.OrderType _OrderType;
        public Utility.OrderStatus _OrderStatus;

        T_OrderInvoice mOrderInvoice = new T_OrderInvoice();
        #endregion

        public frmSaleInvoiceDetailInfo()
        {
            InitializeComponent();
        }

        private void frmBuyToStockDetail_Load(object sender, EventArgs e)
        {
            initForm();

            switch (OpMode)
            {
                case Utility.OperateMode.Query:
                    btnSave.Visible = false;
                    txtOrderID.BackColor = Color.Silver;
                    txtOrderID.Properties.ReadOnly = true;
                    showInfo(_InvoiceID);
                    break;
                case Utility.OperateMode.Insert:
                    txtOrderDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtInvoiceDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

                    txtOrderUserID.EditValue = TierService.Utility.gUserID;
                    txtDepotID.EditValue = ((int)Utility.DepotType.Material).ToString();

                    if (_OrderID != "")
                        txtOrderID.Text = _OrderID;

                    break;
                case Utility.OperateMode.Update:
                    btnSave.Enabled = true;
                    txtOrderID.BackColor = Color.Silver;
                    txtOrderID.Properties.ReadOnly = true;
                    showInfo(_InvoiceID);
                    break;
                case Utility.OperateMode.Delete:
                    break;
                default:
                    break;
            }

        }

        private void initForm()
        {
            //Utility.DataToLookupComboxEx(txtOrderType, C_DMB.GetListByType(C_DMB.DType.OrderType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtAuditUserID, (new P_User()).GetList(""), "UserID", "UserName");
            //Utility.DataToLookupComboxEx(txtBuyUserID, (new P_User()).GetList(""), "UserID", "UserName");

            txtOrderUserID.Enabled = false;
            switch (_OrderStatus)
            {
                case Utility.OrderStatus.S0Input:
                    lblAuditDate.Visible = false;
                    lblAuditUserID.Visible = false;
                    txtAuditDate.Visible = false;
                    txtAuditUserID.Visible = false;
                    btnSubmitAudit.Enabled = false;
                    break;
                case Utility.OrderStatus.S1Auditing:
                    txtAuditDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    txtAuditUserID.EditValue = TierService.Utility.gUserID;
                    break;
                case Utility.OrderStatus.All:
                    break;
                default:
                    break;
            }

        }


        private void showInfo(string sID)
        {
            mOrderInvoice.GetModel(sID);
            this.txtInvoiceID.Text = mOrderInvoice.InvoiceID;
            this.txtOrderID.Text = mOrderInvoice.OrderID;
            if (mOrderInvoice.OrderDate != null)
                this.txtOrderDate.DateTime = mOrderInvoice.OrderDate.Value;
            this.txtOrderUserID.EditValue = mOrderInvoice.OrderUserID;
            //this.txtOrderStatus.EditValue = mOrder.OrderStatus;
            this.txtDepotID.EditValue = mOrderInvoice.DepotID;
            //this.txtToDepotID.EditValue = mOrder.ToDepotID;
            this.txtAuditUserID.EditValue = mOrderInvoice.AuditUserID;
            if (mOrderInvoice.AuditDate != null)
                this.txtAuditDate.DateTime = mOrderInvoice.AuditDate.Value;
            this.txtRemark.Text = mOrderInvoice.Remark;

            //////////---------------Invoice
            this.txtInvoiceRemark.Text = mOrderInvoice.InvoiceRemark;
            this.txtInvoiceNo.Text = mOrderInvoice.InvoiceNo;
            this.txtInvoiceMoney.Text = mOrderInvoice.InvoiceMoney.Value.ToString();
            if (mOrderInvoice.InvoiceDate != null)
                this.txtInvoiceDate.DateTime = mOrderInvoice.InvoiceDate.Value;


            //*****************Order Detail*****************//
            BindOrderDetail();

        }
        private void GetFormReturValue(string str)
        {
            string sID = str;
            if (sID != "")
            {
                showInfo(sID);
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtSaleInvoiceMoney.Value - txtInvoicedMoney.Value < txtInvoiceMoney.Value)
            {
                XtraMessageBox.Show("开票金额不能大于“合同总金额”-“已开票金额”!");
                return;
            }
            if (SaveOrder())
            {
                XtraMessageBox.Show("开票信息保存成功!");
            }
        }

        private bool SaveOrder()
        {
            if (this.txtInvoiceDate.Text == "")
            {
                XtraMessageBox.Show("开票日期不能为空,请录入!");
                txtInvoiceDate.Focus();
                return false;
            }

            mOrderInvoice.InvoiceID = this.txtInvoiceID.Text;
            mOrderInvoice.OrderID = this.txtOrderID.Text;
            mOrderInvoice.OrderDate = DateTime.Parse(this.txtOrderDate.Text);
            mOrderInvoice.OrderUserID = (string)this.txtOrderUserID.EditValue;
            mOrderInvoice.OrderStatus = "0";
            mOrderInvoice.DepotID = (string)this.txtDepotID.EditValue;
            if (_OrderStatus != Utility.OrderStatus.S0Input)
            {
                mOrderInvoice.AuditUserID = (string)this.txtAuditUserID.EditValue;
                mOrderInvoice.AuditDate = DateTime.Parse(this.txtAuditDate.Text);
            }
            mOrderInvoice.Remark = this.txtRemark.Text;

            mOrderInvoice.OrderSort = "1";//销售开票
            mOrderInvoice.InvoiceNo = this.txtInvoiceNo.Text;
            mOrderInvoice.InvoiceMoney = this.txtInvoiceMoney.Value;
            mOrderInvoice.InvoiceDate = this.txtInvoiceDate.DateTime;
            mOrderInvoice.InvoiceRemark = this.txtInvoiceRemark.Text;

            if ((mOrderInvoice.OrderID == ""))
            {
                XtraMessageBox.Show("请选择销售订单编号!");
                txtOrderID.Focus();
                return false;
            }
            bool flag = false;
            switch (OpMode)
            {
                case TierService.Utility.OperateMode.Insert:
                    try
                    {
                        mOrderInvoice.InvoiceID = Utility.GetSeqval(Utility.SeqName.InvoiceID.ToString());
                        mOrderInvoice.Add();
                        _InvoiceID = mOrderInvoice.InvoiceID;

                        txtInvoiceID.Text = _InvoiceID;
                        BindOrderDetail();
                        OpMode = Utility.OperateMode.Update;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("开票信息新增出错,错误原因为:" + ex.Message);
                        break;
                    }
                    //MessageBox.Show("入库单新增成功!");
                    btnSubmitAudit.Enabled = true;
                    flag = true;
                    //DialogResult = DialogResult.OK;
                    break;
                case TierService.Utility.OperateMode.Update:
                    try
                    {
                        // mCustomer.CustID = txtCustID.Text;
                        mOrderInvoice.Update();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("开票信息保存出错,错误原因为:" + ex.Message);
                        break;
                    }


                    btnSubmitAudit.Enabled = true;
                    //MessageBox.Show("入库单信息修改成功!");
                    //DialogResult = DialogResult.OK;
                    flag = true;
                    break;
                default:
                    break;
            }
            return flag;
        }

        private void btnProductAdd_Click(object sender, EventArgs e)
        {

        }


        private void BindOrderDetail()
        {

            if (_InvoiceID == "") return;

         
        }


        private void txtPOrderID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmSaleInvoiceNeed fDetail = new frmSaleInvoiceNeed();
            fDetail.gFuncType = Utility.FuncType.Select;
            fDetail._IsStock = "Y";
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtOrderID.Text = fDetail._OrderID;
                string sPOrderID = txtOrderID.Text;
                //生成商品明细
                //BindSaleShipByOrderID(sPOrderID);
            }
        }

        private void txtPOrderID_EditValueChanged(object sender, EventArgs e)
        {
            string sPOrderID = txtOrderID.Text;
            T_Order mSaleOrder = new T_Order();
            mSaleOrder.GetModel(sPOrderID);
            //txtBuyUserID.EditValue = mSaleOrder.OrderUserID;
            txtDepotID.EditValue = mSaleOrder.DepotID;
            //if (mSaleOrder.OrderDate != null)
            //    txtBuyDate.DateTime = mSaleOrder.OrderDate.Value;
            txtSaleInvoiceMoney.Value = mSaleOrder.PayFee.Value;
            T_Customer mCustomer = new T_Customer();
            mCustomer.GetModel(mSaleOrder.CustID);
            //txtCustName.Text = mCustomer.CustName;
            txtDepotID.EditValue = mSaleOrder.DepotID;

        }




        private void btnAudit_Click(object sender, EventArgs e)
        {

        }

        private void btnOrderPrint_Click(object sender, EventArgs e)
        {
            string sOrderID = txtInvoiceID.Text;
            if (sOrderID != "")
            {
                TdsOrder dsOrder = TOrderFunc.GetOrderPrintList(sOrderID);
                RptOrderBuyToStock xrptOrderBuy = new RptOrderBuyToStock();
                xrptOrderBuy.DataSource = dsOrder;
                xrptOrderBuy.ShowPreviewDialog();
            }
        }

        private void btnSubmitAudit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确认此单据开票信息，确认后将不能更改？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            T_Order mOrder = new T_Order();
            mOrder.Audit(txtInvoiceID.Text, Utility.gUserID,0);
            Close();
        }

        private void btnReturnFee_Click(object sender, EventArgs e)
        {
            //if (gvSaleInvoice.RowCount < 1) return;
            //DataRow dr = gvSaleInvoice.GetDataRow(gvSaleInvoice.FocusedRowHandle);
            //string sID = dr["InvoiceID"].ToString();

            //frmSaleInvoiceReturnFee fDetail = new frmSaleInvoiceReturnFee();
            //fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            //fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            //fDetail._InvoiceID = sID;
            //fDetail._OrderType = Utility.OrderType.SaleReturnFee;
            //fDetail.ShowDialog();

        }

        private void txtOrderID_EditValueChanged(object sender, EventArgs e)
        {
            //T_Order mOrder = new T_Order();
            //mOrder.GetModel(txtOrderID.Text);
            //txtInvoiceMoney.Value = mOrder.InvoiceMoney.Value;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("Select PayFee,(select isnull(Sum(InvoiceMoney),0) from T_OrderInvoice where OrderID=a.OrderID and InvoiceID<>'" + txtInvoiceID.Text + "') InvoicedMoney");
            strSql.Append(" FROM T_Order a ");
            strSql.Append(" where a.OrderID='" + txtOrderID.Text + "'");
            DataSet dsInvoice = DBUtility.DbHelperSQL.Query(strSql.ToString());
            txtSaleInvoiceMoney.Text = dsInvoice.Tables[0].Rows[0]["PayFee"].ToString();
            txtInvoicedMoney.Text = dsInvoice.Tables[0].Rows[0]["InvoicedMoney"].ToString();

        }

    }
}